<?php
/**
 * DB
 */
class Database
{
    public static $lastQuery;
    private static $dbHandle;
    private $dbLink;

    public function getOneResult($sql)/*{{{*/
    {
        $result =   FALSE;
        if (empty($sql))    return  $result;

        self::$lastQuery = $sql;
        $query  =   $this->dbQuery($sql);
        if (empty($query))  return  $result;

        $rows   =   mysql_num_rows($query);
        if (0   ==  $rows)  return  array();

        $result =   mysql_fetch_assoc($query);
        return  $result;
    }/*}}}*/

    public function getManyResult($sql)/*{{{*/
    {
        $result =   FALSE;
        if (empty($sql))    return  $result;

        self::$lastQuery = $sql;
        $query  =   $this->dbQuery($sql);
        if (empty($query))  return  $result;

        $lines  =   mysql_num_rows($query);
        if (empty($lines))  return  array();

        while ($tmp =   mysql_fetch_assoc($query))
        {
            $result[]   =   $tmp;
        }

        return  $result;
    }/*}}}*/

    public function getInsertId()/*{{{*/
    {
        return  mysql_insert_id($this->dbLink);
    }/*}}}*/

    public function getAffectedRows()/*{{{*/
    {
        return  mysql_affected_rows($this->dbLink);
    }/*}}}*/

    public function dbExec($sql)/*{{{*/
    {
        $result =   FALSE;
        if (empty($sql))    return  $result;

        self::$lastQuery = $sql;
        $result =   $this->dbQuery($sql);

        if ($result)
        {
            $lines  =   mysql_affected_rows($this->dbLink);
            if ($lines) $result =   $lines;
        }
        else
        {
            #   $error  =   'No : '. mysql_errno(). "\t". mysql_error(). "\r\n\r\n";
            #   $sqlStr =   'Ti : '. AC::SERVER_TIME. "\t". date('Y/m/d H:i:s', AC::SERVER_TIME). "\t". $sql. "\t". $result. "\r\n";
            #   UK::saveLog('sql.dbExec.txt', $sqlStr. $error, TRUE);
        }
        $sqlStr =   'Ti : '. AC::SERVER_TIME. "\t". date('Y/m/d H:i:s', AC::SERVER_TIME). "\t". $sql. "\t". $result. "\r\n";
        #   UK::saveLog('debug.dbExec.txt', $sqlStr, TRUE);

        return  $result;
    }/*}}}*/

    public function dbQuery($sql)/*{{{*/
    {
        if (empty($sql))            return  FALSE;

        self::$lastQuery = $sql;
        $query  =   mysql_query($sql, $this->dbLink);
        if (empty($query))
        {
            # echo 'sql = ', $sql, '        ;      ';
            # var_dump(mysql_errno());
            $error  =   'No : '. mysql_errno(). "\t". mysql_error(). "\r\n\r\n";
            $sqlStr =   'Ti : '. AC::SERVER_TIME. "\t IP : ". UK::getClientIp(). "\t Date : ". date('Y/m/d H:i:s', AC::SERVER_TIME). "\t". $sql. "\r\n";
            UK::saveLog('sql.error', $sqlStr. $error, TRUE);
            throw new DBException('sql query error. '.$error);
        }
        return  $query;
    }/*}}}*/

    //开启事务，关闭自动提交
    public function begin(){
        return mysql_query("BEGIN", $this->dbLink);
    }

    //提交事务
    public function commit(){
        return mysql_query("COMMIT", $this->dbLink);
    }

    //回滚事务
    public function rollback(){
        return mysql_query("ROLLBACK", $this->dbLink);
    }

    public function dbConnect($dbConfigKey, $hostConfig)/*{{{*/
    {
        if (empty($hostConfig)  ||  empty($hostConfig['host'])) exit ('DB server key is wrong! ');

        if (isset(self::$dbHandle[$dbConfigKey])    &&  is_resource(self::$dbHandle[$dbConfigKey]))
        {
            $this->dbLink   =   self::$dbHandle[$dbConfigKey];
        }
        else
        {
            extract($hostConfig);
            $this->dbLink   =   mysql_connect($host. ':'. $port, $user, $pass);
            if (empty($this->dbLink))   return  NULL;

            mysql_query('SET NAMES UTF8', $this->dbLink);
            # mysql_set_charset('UTF8', $this->dbLink);
            self::$dbHandle[$dbConfigKey]   =   $this->dbLink;
        }

        return  $this->dbLink;
    }/*}}}*/

}

